package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.config.ThreadPoolConfig;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.aspectj.lang.annotation.Around;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Supplier;


@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        HashMap<String, Object> map = new HashMap<>();

        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(()->{
            //1
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo",skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        CompletableFuture<Void> getSkuValueIdsMapCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //2
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String s = JSON.toJSONString(skuValueIdsMap);
            map.put("valuesSkuJson",s);
        },threadPoolExecutor);

        CompletableFuture<Void> categoryView1 = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //3
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        },threadPoolExecutor);


        CompletableFuture<Void> voidCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //4
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList",spuSaleAttrListCheckBySku);
        },threadPoolExecutor);

        CompletableFuture<Void> incrHotScoreCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo -> {
            Result result = listFeignClient.incrHotScore(skuId);
        }), threadPoolExecutor);


        CompletableFuture<Void> price1 = CompletableFuture.runAsync(() -> {
            //1
            BigDecimal price = productFeignClient.getSkuPrice(skuId);
            map.put("price", price);
        },threadPoolExecutor);

        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                getSkuValueIdsMapCompletableFuture,
                categoryView1,
                voidCompletableFuture,
                incrHotScoreCompletableFuture,
                price1
        ).join();
        return map;
    }


}
